/**
* Copyrights 2011 Michal Saman, m.saman@designplus.cz.
* This source code is release under the Apache License 2.0.
* http://www.apache.org/licenses/LICENSE-2.0
*
* @file	 process.h
* @brief Process handling and management
*/
#ifndef PROCESS_H
#define PROCESS_H

#include <env.h>
#include <stdint.h>
#include <proc/pcontext.h>

#define KERNEL_PRIVILEGE		0		/*<< Kernel space privilege */
#define USER_PRIVILEGE			3		/*<< Normal user space privilege */

#define PROC_RUNNING			1		/*<< Process is running */
#define PROC_READY				2		/*<< Process is ready to run */
#define PROC_WAIT				3		/*<< Process is waiting for I/O */
#define PROC_TERMINATED			4		/*<< Process is terminated */

#define PROC_SHED_QUEUES		10		/*<< Count of sheduler priority queutes */

#define PROC_STATS_TICKS		100		/*<< Interval for continuous statistics calculation (in ticks) */

typedef int32_t                 pid_t;	/*<< Process is data type */

typedef struct process {
	pid_t			pid;				/*<< Process ID */
	pcontext_t 		pcontext;			/*<< Process context */
	uint8_t			state;				/*<< Process state */
	char			pname[256];			/*<< Process name */
	uint8_t			privilege;			/*<< Kernel/user space */
	uint8_t			priority;			/*<< Process priority */
	//struct process*	child;			/*<< Child process */
	struct process*	parent;				/*<< Parent process */
	uint32_t		entr_run;			/*<< Tick when process starts last run */
	uint32_t		in_run;				/*<< Count of ticks when process was running */
	uint32_t		entr_rdy_queue;		/*<< Tick when process is wainting in ready queue */
	uint32_t		in_rdy_queue;		/*<< Count of ticks when process was wainting in RDY queue */

	uint32_t		c_entr_run;			/*<< Tick when process starts last run (continous stats) */
	uint32_t		c_in_run;			/*<< Count of ticks when process is running in current interval (continous stats) */
	uint32_t		c_in_run_last;		/*<< Count of ticks when process is running in last interval (continous stats) */
} process_t;

extern	uint32_t	get_pc();
extern	process_t*	init_process(char* pname, int privilege, uint8_t priority, process_t* parent);
extern	void fork_process();
extern 	void proc_info();
static __inline__ void proc_scheduler();
void proc_dispatcher();
extern 	void mt_pit_irq_handler();
static  void proc_info_entry(process_t* p);

#endif

